package com.caixy.adminSystem.config;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Configuration
public class OpenApiUrlConfig implements ApplicationListener<ApplicationReadyEvent> {
    private final Environment environment;

    public OpenApiUrlConfig(Environment environment) {
        this.environment = environment;
    }

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        WebServerApplicationContext applicationContext = (WebServerApplicationContext) event.getApplicationContext();
        int port = applicationContext.getWebServer().getPort();
        String contextPath = environment.getProperty("server.servlet.context-path", ""); // 使用默认值""以防没有设定
        if (contextPath.equals("/")) { // 处理根路径情况
            contextPath = "";
        }
        String openApiUrl = String.format("http://localhost:%d%s/doc.html", port, contextPath);

        System.out.println("OpenAPI文档URL: " + openApiUrl);
    }
}
